package com.redstar.interesting.main.hello.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.redstar.interesting.common.constants.Constants;
import com.redstar.interesting.common.enums.EnumServerCode;
import com.redstar.interesting.common.exception.BaseException;
import com.redstar.interesting.main.hello.listenner.TestListenerParam;
import com.redstar.interesting.main.hello.mapper.HomeworkMapper;
import com.redstar.interesting.main.hello.param.HomeworkParam;
import com.redstar.interesting.main.hello.po.Homework;
import com.redstar.interesting.main.hello.service.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author lihongxing
 */
@Service
public class HelloServiceImpl implements HelloService {
    private static final Logger log = LoggerFactory.getLogger(HelloServiceImpl.class);


    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private HomeworkMapper homeworkMapper;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public Homework selectByTitle(String title) {

        log.info("selectByName入参:{}", title);
        if (!StringUtils.hasText(title)) {
            return new Homework();
        }
//        Homework homework = homeworkMapper.selectOneByTitle(title);
        Homework homework = new Homework();
        homework.setTitle(MDC.get(Constants.TRACE_ID));
        homework.setId(System.currentTimeMillis());
        log.info("查询完成");

        return homework;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addHomework() {
        log.info("addHomework添加作业");
        Homework homework = new Homework();
        homework.setTitle("我是作业标题");
        homework.setBroadcastContentId(97L);
        homework.setClassId(36419L);
        homework.setCourseId(400129L);
        homework.setDemand("没有简介");
        homework.setDemandUrl("没有简介图片");
        homework.setCreatorUid(0L);
        homework.setCreateTime(new Date());
//        int i = homeworkMapper.insertSelective(homework);
//        log.info("添加成功条数：{}",i);
        applicationEventPublisher.publishEvent(new TestListenerParam(homework));
        return 0;

    }

    @Override
    public Integer modifyTitle(HomeworkParam param) {
        log.info("modifyTitle入参:{}", JSONObject.toJSONString(param));
        if (param.getId() == null || param.getId() <= 0 || !StringUtils.hasText(param.getTitle())) {
            throw new BaseException(EnumServerCode.PARAMETER_ERROR, "id错误或者标题错误");
        }
        Homework update = new Homework();
        update.setId(param.getId());
        update.setTitle(param.getTitle());
//        int i = homeworkMapper.updateByPrimaryKeySelective(update);
//        log.info("修改成功条数：{}",i);
        return 0;
    }

    @Override
    public Integer deleteById(Long id) {
        log.info("deleteById入参:id = {}", id);
//        int i = homeworkMapper.deleteByPrimaryKey(id);
//        log.info("删除成功条数：{}",i);
        return 0;
    }

}
